package com.scqkxx.gamebox.activity.game;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.game.adapter.GameAdapter;
import com.scqkxx.gamebox.activity.game.presenter.GamePresenter;
import com.scqkxx.gamebox.activity.game.view.GameView;
import com.scqkxx.gamebox.activity.myself.MyselfEditNicknameActivity;
import com.scqkxx.gamebox.activity.space.SearchActivity;
import com.scqkxx.gamebox.common.Const;
import com.scqkxx.gamebox.databinding.LayoutGameBinding;
import com.scqkzqtz.base.base.BaseFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 游戏
 * Created by hef on 2018/8/10. 11点59分
 * name: ts
 * 用途：游戏首页
 */

public class GameFragment extends BaseFragment<LayoutGameBinding> implements GameView, OnRefreshListener {
    private List<AVObject> mGameListData;
    private GamePresenter mGamePresenter; //游戏页面的控制器
    private GameAdapter mGameAdapter;
    private View view, view2;

    @Override
    public void loadData() {
        binding.mGameSmartRefreshLayout.autoRefresh(0, 250, 1f);
    }

    @Override
    public int setLayoutId() {
        return R.layout.layout_game;
    }

    @Override
    public void init() {
        binding.mGameTitleView.getTv_lift().setVisibility(View.VISIBLE);
        binding.mGameTitleView.setLeftIcon(R.mipmap.icon_more);
        binding.mGameTitleView.getTv_right().setVisibility(View.GONE);
        binding.mGameTitleView.getIv_setting().setImageResource(R.mipmap.icon_search);
        binding.mGameTitleView.setOnTitleClickListener((int titleType) -> {
            if (titleType == TitleView.CLICK_BACK) {
                EventBus.getDefault().post(Const.OPEN_NAV);
            } else if (TitleView.CLICK_SETTING == titleType){
                ActivityUtil.switchTo(_Activity, SearchActivity.class);
            }
        });
        initGameAdapter();
//        initData(); //进行网络请求数据
        initListener();
    }

    //初始化用户感兴趣的游戏的适配器
    private void initGameAdapter() {
        view = inflate(com.scqkzqtz.base.R.layout.layout_error);
        view2 = inflate(com.scqkzqtz.base.R.layout.layout_empty);
        mGamePresenter = new GamePresenter(_Activity, this);
        mGameListData = new ArrayList<>();
        mGameAdapter = new GameAdapter(R.layout.game_item_layout, mGameListData);
        ViewGroup parentView = (ViewGroup) _Activity.getWindow().getDecorView();
        View view = getLayoutInflater().inflate(R.layout.end_layout, parentView, false);
        View view2 = getLayoutInflater().inflate(R.layout.eny_layout, parentView, false);
        mGameAdapter.addFooterView(view);
        mGameAdapter.setEmptyView(view2);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(_Activity);
        binding.mGameRecyclerView.setLayoutManager(linearLayoutManager);
        binding.mGameRecyclerView.setAdapter(mGameAdapter);
        binding.mGameSmartRefreshLayout.setEnableLoadMore(false);
    }

    //设置监听
    private void initListener() {
        binding.mGameSmartRefreshLayout.setOnRefreshListener(this);
    }

    //请求数据监听
    private void initData() {
        AVQuery<AVObject> query = AVQuery.getQuery("Goods");
        query.whereEqualTo("isDisable", 0);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                if (avException == null) {
                    mGameListData.clear();
                    mGameListData.addAll(avObjects);
                    mGameAdapter.setNewData(mGameListData);
                    onFinishFreshAndLoad();
                }
            }
        });
    }

    //关闭刷新
    @Override
    public void onFinishFreshAndLoad() {
        binding.mGameSmartRefreshLayout.finishRefresh(true);
    }

    //清除数据
    @Override
    public void clearActiveList() {
        mGameListData.clear();
    }

    //刷新失败
    @Override
    public void onActiveFail(AVException e) {
        binding.mGameSmartRefreshLayout.finishRefresh(false);
    }

    //添加全部游戏
    @Override
    public void onGameAllList(List<AVObject> list) {
        mGameListData.addAll(list);
    }

    @Override
    public void notifyDataSetChanged() {
        mGameAdapter.notifyDataSetChanged();
    }

    //设置null布局
    @Override
    public void setEmptyStateView() {
//        mGameAdapter.setEmptyView(view2);
    }

    @Override
    public int getListSize() {
        return mGameListData.size();
    }

    //网络异常的布局
    @Override
    public void setErrorStateView() {
//        mGameAdapter.setEmptyView(view);
    }

    //刷新数据
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

}
